import {
    getSblxBySbzlx
} from "../../../../../mapUtils/SblxUtil";
import IpmsLineString from "../IpmsLineString";
import RuleResult from "../vo/RuleResult";

/**
 * 架空线路修改
 * @implements {IpmsLineString}
 */
class PmsJkxl extends IpmsLineString {
    constructor(_) {
        super(_);
        this.defaultSbzlx = 10100000;
        this.defaultDydj = 22;
        this.drawtype = "pms_jkxl";
        this.defaultName = "新增架空线路";
        this.defaultLineColor = "#ff0000";
        this.defaultLineWidth = 3;
        this.defaultLineOpacity = 1;
    }

    /**
     * 新增架空线路
     * @override
     * @param {Object} option 默认参数
     * @param {Array} snapList 捕捉图形
     * @param {Array} ignoreSbzlx 捕捉图形中不包含该数组中设备子类型设备
     */
    draw(option, snapList, ignoreSbzlx) {
        this.option = option;

        this.properties = Object.assign({}, this.properties, this.option);
        return super.draw(option, snapList, ignoreSbzlx);
    }

    /**
     * 编辑架空线路
     * @override
     * @param {Object} option 默认参数
     * @param {Array} snapList 捕捉图形
     * @param {Array} ignoreSbzlx 捕捉图形中不包含该数组中设备子类型设备
     */
    edit(option, snapList, ignoreSbzlx) {
        this.option = option;
        return super.edit(option, snapList, ignoreSbzlx);
    }

    /**
     * 获取当前绘制设备信息
     * @override
     */
    async getTargetDevice() {
        let startEquip = null,
            endEquip = null;
        let start = this.getStartDevice();
        let end = this.getEndDevice();
        if (start) {
            // 起始设备为其他设备
            startEquip = {
                oid: start.oid,
                sbmc: start.sbmc,
                sblx: start.sblx,
                sbzlx: start.sbzlx,
                versionid: start.versionid,
                dydj: start.dydj,
            };
        }
        if (end) {
            // 终点设备为其他设备
            endEquip = {
                oid: end.oid,
                sbmc: end.sbmc,
                sblx: end.sblx,
                sbzlx: end.sbzlx,
                versionid: end.versionid,
                dydj: start.dydj,
            };
        } else if (!this.editFlags.status) {
            endEquip = {};
        }
        let data = {
            startEquip: startEquip,
            endEquip: endEquip,
            schema: this.option.schema,
            ghrw: this.option.ghrw || "",
            ghfa: this.option.ghfa || "",
            ghnf: this.option.ghnf || "",
            ghxm: this.option.ghxm || "",
            fxlx: this.option.fxlx || 2,
            oid: this.option.oid || "",
            away: 0,
            sblx: getSblxBySbzlx(this.defaultSbzlx),
            sbzlx: this.defaultSbzlx,
            otherfields: {
                sbmc: this.option.sbmc || this.defaultName,
                shape: this.getGeometryWkt(this.geometry),
                dxcd: turf.length(this.geometry),
                dydj: this.option.dydj || this.defaultDydj,
                ssqy: this.option.ssqy || "",
                ssdkx: this.option.ssdkx || "",
            },
        };
        if (this.editFlags.status === "editShape") {
            data.shapeList = [{
                oid: this.option.oid,
                sbzlx: this.option.sbzlx,
                blockid: this.option.blockid,
                versionid: this.option.versionid,
                shape: data.otherfields.shape,
            }, ];
        }
        return data;
    }

    /**
     * 验证点合理性
     * @override
     * @param {Number} index 点下标
     * @param {*} snap 捕捉到的设备
     */
    async _ruleJudgeDrawPoint(index, snap) {
        let layer = snap ? snap.layer : null;
        if (!layer) {
            // 如果是空点
            if (index === 0) {
                // 如果是第一个点，不能为空
                return new RuleResult(false, index, "起点设备为空");
            }
        }
        return new RuleResult(true, index, "success");
    }

    /**
     * 验证编辑点的合理性
     * @override
     * @param {*} snap 捕捉到的设备
     */
    async _ruleJudgeEditPoint(snap) {
        if (!snap) {
            return new RuleResult(false, 0, "连接设备为空");
        }
        // 如果不是空点
        if (this._isStation(snap.properties.sbzlx)) {
            // 先判断是否是电站，判断电站间隔是否已用完
            // let isDzjgAvailable = await this._isDzjgAvailable(snap.properties, this.defaultDydj);
            // if (isDzjgAvailable) {
            return new RuleResult(true, 0, "success");
            // } else {
            //   return new RuleResult(false, 0, "电站无可用间隔，无法进行线路录入");
            // }
            // } else if (!this._isPermittedDevice(layer.properties.sbzlx)) {
            //   // 然后判断是否是环网柜和分支箱
            //   return new RuleResult(false, index, "架空线路不能连接分支箱和环网柜");
        } else if (this._isLine(snap.properties.sbzlx)) {
            // 再判断是否是线路，线路是否有足够线段T接
            let segments = snap.properties.ysb_oid.split(";");
            if (segments.length <= 1) {
                return new RuleResult(false, 0, "线路没有多余连接点进行T接");
            }
            return new RuleResult(true, 0, "success");
        }
        // 其它情况直接通过
        return new RuleResult(true, 0, "success");
    }

    /**
     * 验证所有点合理性
     * @override
     */
    _ruleJudgeAllPoint() {
        console.log("_ruleJudgeAllPoint", this.snaps);
        /**
      sbzlx     sblx    tablename
      10200002  102000  t_fz_zwyc_yxg 规划虚拟点设备(T接点)
      30000004  300000  t_fz_znyc_dz 开关站
      32400000  300000  t_fz_znyc_dz 环网柜
      20400000  300000  t_fz_znyc_dz 站房-电缆分支箱
      30000005  300000  t_fz_znyc_dz 站内-配电站
      32300000  300000  t_fz_znyc_dz 箱式变电站
      30000099  300000  t_fz_znyc_dz 水电站
      30000003  300000  t_fz_znyc_dz 发电厂
      30000010  300000  t_fz_znyc_dz 太阳能电厂
      30000002  300000  t_fz_znyc_dz 风电厂
      34300000  300000  t_fz_znyc_dz 用户变电站
      30000006  300000  t_fz_znyc_dz 用户站
      30000001  300000  t_fz_znyc_dz 换流站
      30000000  300000  t_fz_znyc_dz 变电站
    **/

        const EnableStartSbzlx = [10200002, 30000004, 32400000, 20400000, 30000005, 32300000, 30000099, 30000003, 30000010, 30000002, 34300000, 30000006, 30000001, 30000000];
        const EnableEndSbzlx = [10200002, 30000004, 32400000, 20400000, 30000005, 32300000, 30000099, 30000003, 30000010, 30000002, 34300000, 30000006, 30000001, 30000000];
        let start = this.snaps[0];
        let end = this.snaps[this.snaps.length - 1];
        if (this.option.dydj + "" === "22") {
            // 10kv线路
            // 判断是否连接起点
            if (!start) {
                return new RuleResult(false, 0, "起点必须连接设备");
            }
            var startSbzlx = start.properties.sbzlx;
            // 已连接起点，判断起点设备类型是否正确
            if (EnableStartSbzlx.indexOf(startSbzlx) === -1) {
                return new RuleResult(false, 0, "起点设备类型不正确");
            }
            // 判断是否连接终点
            if (!end) {
                // 可以不连接
                return new RuleResult(true, 0, "success");
            }
            var endSbzlx = end.properties.sbzlx;
            // 如果连接，则需要判断终点设备类型是否正确
            if (EnableEndSbzlx.indexOf(endSbzlx) === -1) {
                return new RuleResult(false, 0, "终点设备类型不正确");
            }
            return new RuleResult(true, 0, "success");
        }
        // 判断是否连接起点
        if (!start) {
            return new RuleResult(false, 0, "起点必须连接设备");
        }
        startSbzlx = start.properties.sbzlx;
        // 已连接起点，判断起点设备类型是否正确
        if (EnableStartSbzlx.indexOf(startSbzlx) === -1) {
            return new RuleResult(false, 0, "起点设备类型不正确");
        }
        // 判断是否连接终点
        if (!end) {
            // 必须连接终点设备
            return new RuleResult(false, 0, "终点必须连接设备");
        }
        endSbzlx = end.properties.sbzlx;
        // 如果连接，则需要判断终点设备类型是否正确
        if (EnableEndSbzlx.indexOf(endSbzlx) === -1) {
            return new RuleResult(false, 0, "终点设备类型不正确");
        }
        return new RuleResult(true, 0, "success");
    }
}

export default PmsJkxl;